package com.amigo.online.provider.user.util;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import com.amigo.online.common.core.response.StatusCode;
import com.amigo.online.common.param.dto.user.UserNotifyDto;
import com.amigo.online.common.param.result.HobbyInfo;
import com.amigo.online.common.param.result.HobbyUserInfo;
import com.amigo.online.common.param.result.PhotoInfo;
import com.amigo.online.common.param.result.UserIndexInfo;
import com.amigo.online.common.param.result.UserInfo;
import com.amigo.online.provider.user.exception.UserException;
import com.amigo.online.provider.user.model.entity.Black;
import com.amigo.online.provider.user.model.entity.User;
import com.amigo.online.provider.user.model.entity.UserHobby;
import com.amigo.online.provider.user.model.entity.UserPhotos;

public class ConvertUtil {

    public static UserInfo UserToUserInfo(User user) {
        if (user == null) {
            return null;
        }
        UserInfo u = new UserInfo();
        u.setId(user.getId());
        u.setAccount(user.getAccount());
        u.setAccountDefault(user.getAccountDefault());
        u.setNickname(user.getNickname());
        u.setEmail(user.getEmail());
        u.setMobilephone(user.getMobilephone());
        u.setCountryCode(user.getCountryCode());
        u.setSex(user.getSex());
        u.setCountry(user.getCountry());
        u.setProvince(user.getProvince());
        u.setCity(user.getCity());
        u.setCompany(user.getCompany());
        u.setJob(user.getJob());
        u.setSchool(user.getSchool());
        u.setBirthday(user.getBirthday());
        u.setLove(user.getLove());
        u.setSign(user.getSign());
        u.setVip(user.getVip());
        u.setVerified(user.getVerified());
        u.setVerifiedTime(user.getVerifiedTime());
        u.setAvatar(user.getAvatar());
        u.setBackground(user.getBackground());
        u.setCelebrity(user.getCelebrity());
        u.setHomeCountry(user.getHomeCity());
        u.setHomeProvince(user.getHomeProvince());
        u.setHomeCity(user.getHomeCity());
        u.setSecret(user.getSecret());
        u.setFacebookOpenId(user.getFacebookOpenId());
        u.setNotifyFollow(user.getNotifyFollow());
        u.setNotifyComment(user.getNotifyComment());
        u.setNotifyGroup(user.getNotifyGroup());
        u.setNotifyZan(user.getNotifyZan());
        u.setNotifyVoice(user.getNotifyVoice());
        u.setHome(user.getHome());
        u.setSexModified(user.getSexModified());
        u.setOpenT(user.getOpenT());
        u.setAvatarSmall(user.getAvatarSmall());
        u.setCreateTime(user.getCreatedTime());
        u.setType(user.getType());
        return u;
    }

    public static PhotoInfo PhotoToPhotoInfo(UserPhotos up) {
        if (up == null) {
            return null;
        }
        PhotoInfo p = new PhotoInfo();
        p.setCheck(up.getChecked());
        p.setCreatedTime(up.getCreatedTime());
        p.setId(up.getId());
        p.setPhoto(up.getPhoto());
        p.setSort(up.getSort());
        p.setFirstImg(up.getFirstImg());
        p.setUserId(up.getUserId());
        return p;
    }

    public static List<UserInfo> UsersToUserInfos(List<User> userList) {
        if (userList == null) {
            return null;
        }
        List<UserInfo> userInfoList = new ArrayList<UserInfo>();
        userInfoList = userList.stream().map(e ->
                new UserInfo(e.getId(), e.getAccount(), e.getAccountDefault(), e.getMobilephone(), e.getNickname(),
                        e.getSex(), e.getEmail(), e.getCountry(), e.getProvince(), e.getCity(), e.getCompany(),
                        e.getJob(), e.getSchool(), e.getBirthday(), e.getLove(), e.getSign(), e.getVip(),
                        e.getVerified(), e.getVerifiedTime(), e.getCelebrity(), e.getAvatar(), e.getBackground(),
                        e.getHomeCountry(), e.getHomeProvince(), e.getHomeCity(), null, e.getSecret(), e.getFacebookOpenId(),
                        e.getNotifyZan(), e.getNotifyComment(), e.getNotifyFollow(), e.getNotifyGroup(), e.getNotifyVoice(),
                        e.getHome(), e.getAvatarSmall())
        ).collect(Collectors.toList());
        return userInfoList;
    }


    public static void check(UserNotifyDto dto) {
        if (dto == null
                || dto.getNotifyZan() == null
                || dto.getNotifyComment() == null
                || dto.getNotifyFollow() == null
                || dto.getNotifyGroup() == null
                || dto.getNotifyVoice() == null) {
            throw new UserException(StatusCode.BAD_REQUEST);
        }
        checkid(dto.getNotifyZan());
        checkid(dto.getNotifyComment());
        checkid(dto.getNotifyFollow());
        checkid(dto.getNotifyGroup());
        checkid(dto.getNotifyVoice());
    }

    public static void checkid(Integer notify) {
        if (notify.intValue() == 1) {
            return;
        } else if (notify.intValue() == 2) {
            return;
        } else {
            throw new UserException(StatusCode.BAD_REQUEST);
        }
    }

    public static void check(Integer id) {
        if (id == null) {
            throw new UserException(StatusCode.BAD_REQUEST);
        }
        if (id == 0) {
            return;
        } else if (id == 1) {
            return;
        } else {
            throw new UserException(StatusCode.BAD_REQUEST);
        }
    }

    public static List<Long> BlackToBlackUserInfo(List<Black> blacklist) {
        if (blacklist == null) {
            return null;
        }
        List<Long> list = new ArrayList<Long>();
        for (Black black : blacklist) {
            list.add(black.getBlackUserId());
        }
        return list;
    }

    public static List<PhotoInfo> UserPhotosToPhotoInfo(List<UserPhotos> list) {
        if (list == null) {
            return null;
        }
        List<PhotoInfo> plist = new ArrayList<>();
        for (UserPhotos userPhotos : list) {
            PhotoInfo pi = new PhotoInfo();
            pi.setCheck(userPhotos.getChecked());
            pi.setCreatedTime(userPhotos.getCreatedTime());
            pi.setId(userPhotos.getId());
            pi.setPhoto(userPhotos.getPhoto());
            pi.setSort(userPhotos.getSort());
            pi.setFirstImg(userPhotos.getFirstImg());
            pi.setUserId(userPhotos.getUserId());
            plist.add(pi);
        }
        return plist;
    }

    public static List<PhotoInfo> UserPhotosToPhotoInfo2(List<UserPhotos> list) {
        if (list == null) {
            return null;
        }
        List<PhotoInfo> plist = new ArrayList<>();
        for (UserPhotos userPhotos : list) {
            if (userPhotos.getChecked() != 2) {
                PhotoInfo pi = new PhotoInfo();
                pi.setCheck(userPhotos.getChecked());
                pi.setCreatedTime(userPhotos.getCreatedTime());
                pi.setId(userPhotos.getId());
                pi.setPhoto(userPhotos.getPhoto());
                pi.setSort(userPhotos.getSort());
                pi.setFirstImg(userPhotos.getFirstImg());
                pi.setUserId(userPhotos.getUserId());
                plist.add(pi);
            }
        }
        return plist;
    }

    public static ArrayList<HobbyInfo> HobbyToHobbyInfo(List<UserHobby> userhobby) {
        ArrayList<HobbyInfo> hobbyinfoList = new ArrayList<>();
        HobbyInfo hobbyInfo1 = new HobbyInfo(1);
        HobbyInfo hobbyInfo2 = new HobbyInfo(2);
        HobbyInfo hobbyInfo3 = new HobbyInfo(3);
        HobbyInfo hobbyInfo4 = new HobbyInfo(4);
        HobbyInfo hobbyInfo5 = new HobbyInfo(5);
        HobbyInfo hobbyInfo6 = new HobbyInfo(6);
        ArrayList<HobbyUserInfo> list1 = new ArrayList<>();
        ArrayList<HobbyUserInfo> list2 = new ArrayList<>();
        ArrayList<HobbyUserInfo> list3 = new ArrayList<>();
        ArrayList<HobbyUserInfo> list4 = new ArrayList<>();
        ArrayList<HobbyUserInfo> list5 = new ArrayList<>();
        ArrayList<HobbyUserInfo> list6 = new ArrayList<>();
        if (userhobby == null) {
            hobbyInfo1.setHobbys(list1);
            hobbyInfo2.setHobbys(list2);
            hobbyInfo3.setHobbys(list3);
            hobbyInfo4.setHobbys(list4);
            hobbyInfo5.setHobbys(list5);
            hobbyInfo6.setHobbys(list6);
            hobbyinfoList.add(hobbyInfo1);
            hobbyinfoList.add(hobbyInfo2);
            hobbyinfoList.add(hobbyInfo3);
            hobbyinfoList.add(hobbyInfo4);
            hobbyinfoList.add(hobbyInfo5);
            hobbyinfoList.add(hobbyInfo6);
            return hobbyinfoList;
        }
        for (UserHobby hobby : userhobby) {
            HobbyUserInfo hu = new HobbyUserInfo();
            hu.setId(hobby.getHobby().getId());
            hu.setHobbyEn(hobby.getHobby().getHobbyEn());
            hu.setHobbyEs(hobby.getHobby().getHobbyEs());
            hu.setHobbyPt(hobby.getHobby().getHobbyPt());
            hu.setHobbyZh(hobby.getHobby().getHobbyZh());
            hu.setDeleted(hobby.getDeleted());
            switch (hobby.getHobby().getHobbyType()) {
                case 1:
                    list1.add(hu);
                    break;
                case 2:
                    list2.add(hu);
                    break;
                case 3:
                    list3.add(hu);
                    break;
                case 4:
                    list4.add(hu);
                    break;
                case 5:
                    list5.add(hu);
                    break;
                case 6:
                    list6.add(hu);
                    break;
            }
        }
        hobbyInfo1.setHobbys(list1);
        hobbyInfo2.setHobbys(list2);
        hobbyInfo3.setHobbys(list3);
        hobbyInfo4.setHobbys(list4);
        hobbyInfo5.setHobbys(list5);
        hobbyInfo6.setHobbys(list6);
        hobbyinfoList.add(hobbyInfo1);
        hobbyinfoList.add(hobbyInfo2);
        hobbyinfoList.add(hobbyInfo3);
        hobbyinfoList.add(hobbyInfo4);
        hobbyinfoList.add(hobbyInfo5);
        hobbyinfoList.add(hobbyInfo6);
        return hobbyinfoList;
    }

    public static UserIndexInfo UserInfoToUserIndexInfo(UserInfo user) {
        if (user == null) {
            return null;
        }
        UserIndexInfo u = new UserIndexInfo();
        u.setId(user.getId());
        u.setAccount(user.getAccount());
        u.setAccountDefault(user.getAccountDefault());
        u.setNickname(user.getNickname());
        u.setEmail(user.getEmail());
        u.setMobilephone(user.getMobilephone());
        u.setCountryCode(user.getCountryCode());
        u.setSex(user.getSex());
        u.setCountry(user.getCountry());
        u.setProvince(user.getProvince());
        u.setCity(user.getCity());
        u.setCompany(user.getCompany());
        u.setJob(user.getJob());
        u.setSchool(user.getSchool());
        u.setBirthday(user.getBirthday());
        u.setLove(user.getLove());
        u.setSign(user.getSign());
        u.setVip(user.getVip());
        u.setVerified(user.getVerified());
        u.setVerifiedTime(user.getVerifiedTime());
        u.setAvatar(user.getAvatar());
        u.setBackground(user.getBackground());
        u.setCelebrity(user.getCelebrity());
        u.setHomeCountry(user.getHomeCity());
        u.setHomeProvince(user.getHomeProvince());
        u.setHomeCity(user.getHomeCity());
        u.setSecret(user.getSecret());
        u.setFacebookOpenId(user.getFacebookOpenId());
        u.setNotifyFollow(user.getNotifyFollow());
        u.setNotifyComment(user.getNotifyComment());
        u.setNotifyGroup(user.getNotifyGroup());
        u.setNotifyZan(user.getNotifyZan());
        u.setNotifyVoice(user.getNotifyVoice());
        u.setHome(user.getHome());
        u.setSexModified(user.getSexModified());
        u.setOpenT(user.getOpenT());
        u.setAvatarSmall(user.getAvatarSmall());
        u.setPhotos(user.getPhotos());
        u.setHobbylist(user.getHobbylist());
        return u;
    }

}